package com.dzl.render;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;
/**
 * 
 * @Description:操作excel
 * @author liufan
 * @time:2021年4月24日 下午3:58:51
 */
public class PoiRender {
	private static final Logger logger = Logger.getLogger(PoiRender.class.getName());
	protected HttpServletRequest request;
	protected HttpServletResponse response;
	
	private List<Map<String,Object>> data;
	private String[] columns;
	private String[] headers;
	private String fileName;
	private String sheetName;

	public PoiRender() {
	}

	public PoiRender(HttpServletRequest request, HttpServletResponse response, String fileName, String sheetName, String[] headers, List<Map<String,Object>> data) {
        this.fileName = fileName;
        this.headers = headers;
        this.data = data;
        this.request = request;
        this.response = response;
        this.render();
    }
	
	public PoiRender(HttpServletRequest request, HttpServletResponse response, String fileName, String sheetName, String[] headers, String[] columns, List<Map<String,Object>> data) {
		try {
			fileName = new String(fileName.getBytes("gb2312"), "iso8859-1");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		this.fileName = fileName;
		this.sheetName = sheetName;
		this.headers = headers;
		this.columns = columns;
		this.data = data;
		this.request = request;
        this.response = response;
		this.render();
	}

	public void render() {

		response.setHeader("Content-disposition", "attachment; filename="+fileName);
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        
		OutputStream os = null;
		try {
			os = response.getOutputStream();
			HSSFWorkbook wb = PoiKit.export(sheetName, 0, headers, columns, data, 20);
			wb.write(os);
		} catch (IOException e) {
			 logger.error(e.getMessage(), e);
		}finally {
            try {
                os.flush();
                os.close();
            } catch (IOException e) {
                logger.error(e.getMessage(), e);
            }

        }
	}

	public List<Map<String, Object>> getData() {
		return data;
	}

	public void setData(List<Map<String, Object>> data) {
		this.data = data;
	}

	public String[] getColumns() {
		return columns;
	}

	public void setColumns(String[] columns) {
		this.columns = columns;
	}

	public String[] getHeaders() {
		return headers;
	}

	public void setHeaders(String[] headers) {
		this.headers = headers;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public String getSheetName() {
		return sheetName;
	}

	public void setSheetName(String sheetName) {
		this.sheetName = sheetName;
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public HttpServletResponse getResponse() {
		return response;
	}

	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}
}
